package com.bjsxt.config.shiro;

import cn.hutool.core.lang.UUID;
import com.bjsxt.constants.Constants;
import org.apache.shiro.util.StringUtils;
import org.apache.shiro.web.session.mgt.DefaultWebSessionManager;
import org.apache.shiro.web.util.WebUtils;

import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import java.io.Serializable;

/**
 * @ClassName TokenWebSessionManager
 * @Description TODO
 * @Author xyx
 */
public class TokenWebSessionManager extends DefaultWebSessionManager {

    @Override
    protected Serializable getSessionId(ServletRequest request, ServletResponse response) {
//        从头里面得到请求的token，如果不存在就生成一个
        String token = WebUtils.toHttp(request).getHeader(Constants.TOKEN);
        if (StringUtils.hasText(token)) {
            return token;
        } else {
            return UUID.randomUUID().toString();
        }
    }
}
